﻿using Biotech.Utils;
using project.B12.IService;
using project.B12.Model.BaseModel;
using project.B12.Model.Common;
using project.B12.Model.Enums;
using project.B12.Model.Models;
using software.Common.MVVM;
using software.dll.user;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace project.B12.Service.UserService
{
    public partial class UserService : ObservableObject, IUserService
    {
        public Dictionary<string, ExpirationTimeEnum> ExpirationTimeEnumList { get;} = EnumUtil.GetEnumDisplay<ExpirationTimeEnum>(typeof(ExpirationTimeEnum));//有效期下拉框清单
        #region Method
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <returns></returns>
        public MessageModel AddUser(string name, string password, RoleEnum? roleName, ExpirationTimeEnum? newUserExpirationTime, string department = "0")
        {
            try
            {
                var flag = _userManager.AddUser(name, password, EnumUtil.GetEnumDes(roleName), department);
                if (flag)
                {
                    if (newUserExpirationTime != ExpirationTimeEnum.不过期)
                    {
                        var flag1 = _userManager.UpdateUserExpirationTime(name, DateTime.Now.AddDays((int)newUserExpirationTime));
                        if (flag1)
                        {
                            GetAllUsers();
                            return MessageModel.ReturnSuccess();//成功
                        }
                        else
                        {
                            return MessageModel.ReturnFail("Failed to modify expiration time");
                        }
                    }
                    GetAllUsers();
                    return MessageModel.ReturnSuccess();//成功
                }
                else
                    return MessageModel.ReturnFail("Failed to add user");
            }
            catch (Exception ex)
            {
                return MessageModel.ReturnFail(ex.ToString());//失败
            }
        }
        #endregion
    }
}
